/*
 * @Author: ૮₍ ˙О˙₎ა
 * @Date: 2023-05-30 09:38:48
 * @Description: 添加和修改组件
 */
import { Button, Card, Form, Input,message } from 'antd'
import React, { useEffect } from 'react'
import { SearchOutlined } from '@ant-design/icons'
import { useForm } from 'antd/lib/form/Form'
import { AddSaveHospital, ReqGetHospitalItem, ReqUpdateHospitalSetResponse } from '@/api/hospital/hospitalSet'
import { useNavigate, useParams } from 'react-router-dom'

export default function AddOrUpdateHospital() {
    // 获取修改数据的id
    let {id}=useParams();
    // 获取form Hook
    let [form]=useForm()
    let navigate=useNavigate()
    // 点击添加按键
    let finish=async ()=>{
        let values=form.getFieldsValue();
        if(id){
                // 如果存在id就是修改操作
            await ReqUpdateHospitalSetResponse({...values,id});
            message.success('修改成功')
        }else{
            await AddSaveHospital(values)
            message.success('添加成功')
        }
        // 添加成功跳转回医院设置页
        navigate('/syt/hospital/hospitalSet')
    }
    useEffect(()=>{
        async function main(){
            let result=await ReqGetHospitalItem(id as string)
            form.setFieldsValue(result)
        }
        main()
    })
    return (
        <Card>
            {/* 添加医院 */}
            <Form
            form={form}
            onFinish={finish}
                labelCol={{ span: 2 }}
                wrapperCol={{ span: 22 }}
            >
                <Form.Item
                    label="医院名称"
                    name="hosname"
                    rules={[{ required: true, message: '医院名称不能为空!' }]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="医院编号"
                    name="hoscode"
                    rules={[{ required: true, message: '医院编号不能为空!' }]}
                >
                    <Input/>
                </Form.Item>
                <Form.Item
                    label="api基础路径"
                    name="apiUrl"
                    rules={[{ required: true, message: 'api基础路径不能为空!' }]}
                >
                    <Input/>
                </Form.Item>
                <Form.Item
                    label="联系人姓名"
                    name="contactsName"
                    rules={[{ required: true, message: '联系人姓名不能为空!' },
                            {pattern:/^[\u4e00-\u9fa5]{0,}$/,message:'请输入正确的联系人姓名【中文】'}
                ]}
                >
                    <Input/>
                </Form.Item>
                <Form.Item
                    label="联系人手机"
                    name="contactsPhone"
                    rules={[{ required: true, message: '联系人手机不能为空!' },
                            {pattern:/^1([358][0-9]|4[456789]|6[67]|7[0135678]|9[1589])\d{8}$/,message:'请输入正确的联系手机号'}
                ]}
                >
                    <Input/>
                </Form.Item>

                <Form.Item wrapperCol={{ offset: 2, span: 22 }}>
                    <Button type="primary" htmlType="submit">
                        保存
                    </Button>
                    <Button className='ml' onClick={()=>{
                        navigate('/syt/hospital/hospitalSet')
                    }}>
                        返回
                    </Button>
                </Form.Item>
            </Form>
        </Card>
    )
}
